﻿Public Class frm_ListUsers
    Dim filename As String
    Private Sub btnBrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrow.Click
        'ខាងក្រោម​ជា​កូដ​សំរាប់​ទាញ​យក​រូបភាព​

        Me.PicUsers.BackgroundImage = Nothing
        Me.PicUsers.Image = Nothing
        OpendImage.Title = "Open Picture Of Student"
        OpendImage.Filter = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif|Bitmaps (*.png)|*.png"
        OpendImage.FilterIndex = 1
        OpendImage.ShowDialog()
        filename = OpendImage.FileName
        PicUsers.Text = filename
        If filename = "" Then   ' ពេល​ទាញ​យក​រូប​មិន​បាន​ឲ្យ​វា​ចាក​ចេញ​
            Exit Sub
        Else
            'ទាញ​យក​រូប​ភាព​មក​ដាក់​លើ​ Picture Box
            PicUsers.BackgroundImage = Image.FromFile(filename)
            PicUsers.BackgroundImageLayout = ImageLayout.Stretch
            Dim des As String = Application.StartupPath & "\UserImages\" & "Users" & DateString & TimeOfDay.Hour & TimeOfDay.Minute & TimeOfDay.Second & ".png"
            LblImageFile.Text = Trim("Users" & DateString & TimeOfDay.Hour & TimeOfDay.Minute & TimeOfDay.Second & ".png")
            System.IO.File.Copy(filename, des, True)
        End If
    End Sub
    Private Sub frm_ListUsers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'ភ្ជាប់​ទៅ​កាន់​ DataBase Access
        Connect()
        'LoadDataToLVUsers("SELECT * FROM tbl_users WHERE Status <> 1", LVUsers)
        LoadDataToLVUsers("SELECT * FROM tbl_users WHERE Deleted <>1 AND Status <> 0", LVUsers)
    End Sub
    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If (Me.txtFullName.Text = "") Then
            MsgBox("Please Enter Value to this!", MsgBoxStyle.Information)
            Me.txtFullName.Focus()
        ElseIf Me.txtLogin.Text = "" Then
            MsgBox("Please Enter Value to Login !", MsgBoxStyle.Information)
            Me.txtLogin.Focus()
        ElseIf (Me.txtPassword.Text = "") Then
            MsgBox("Please Enter Password!", MsgBoxStyle.Information)
        ElseIf Me.cboGroup.Text = "" Then
            MsgBox("Please Select Group!", MsgBoxStyle.Information)
            cboGroup.Focus()
        ElseIf filename = "" Then
            MsgBox("Please Select Image", MsgBoxStyle.Information)

        Else
            Dim mySQL As String = "INSERT INTO tbl_users(UserID,UserName,UserLogin,Gender,[Password],CreateDate,UpdateDate,ImageLink,[Group])" & _
                                    "VALUES(" & Me.txtUserID.Text & ",'" & Me.txtFullName.Text & "','" & Me.txtLogin.Text & "','" & _
                                    Me.cboGender.Text & "','" & Me.txtPassword.Text & "','" & FormatDateTime(Now, DateFormat.ShortDate) & "','" & _
                                    FormatDateTime(Now, DateFormat.ShortDate) & "','" & Me.LblImageFile.Text & "','" & Me.cboGroup.Text & "')"

            SaveDataToAccess(mySQL) 'Call Sub Procedure រក្សាទុក​ទិន្នន័យ​ទៅ​ក្នុង​Access Database
            Clear()   'Call Sub Procedure សំអាត​ទិន្ន​ន័យ​នៅ​លើ​ Control Form
LoadDataToLVUsers("SELECT * FROM tbl_users WHERE Deleted <>1 AND Status <> 0", LVUsers)
        End If



    End Sub
    Private Sub SearchDataFromAccess(ByVal IDSearch As Integer)
        Try
            Dim cn As New OleDb.OleDbConnection
            cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\students.accdb"
            cn.Open()
            Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM tbl_users WHERE UserID=" & IDSearch, cn)
            Dim dt As New DataTable
            da.Fill(dt)
            If dt.Rows.Count > 0 Then

                Me.txtUserID.Text = dt.Rows(0).Item("UserID")
                Me.txtFullName.Text = dt.Rows(0).Item("UserName")
                Me.txtLogin.Text = dt.Rows(0).Item("UserLogin")
                Me.cboGender.Text = dt.Rows(0).Item("Gender")
                Me.cboGroup.Text = dt.Rows(0).Item("Group")
                PicUsers.BackgroundImage = Image.FromFile(Application.StartupPath & "\UserImages\" & dt.Rows(0).Item("ImageLink"))
                PicUsers.BackgroundImageLayout = ImageLayout.Stretch
            End If
            cn.Close()
        Catch ex As Exception
            MsgBox(ex, MsgBoxStyle.Information, "Student Management System Error")
        End Try
    End Sub
    Private Sub btn_New_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_New.Click
        Clear() '​ គឺ​ជា​ Sub ​សំរាប់​សំអាត​ទិន្នន័យ​នៅ​លើ Control របស់​ Form
        Me.txtUserID.Text = AutoID4Access("SELECT UserID FROM tbl_users", "tbl_users") + 1
        Me.txtFullName.Focus()
        Me.BtnEdit.Enabled = False
        btnDelete.Enabled = False
        BtnSave.Enabled = True

    End Sub
    Private Sub Clear()
        Me.txtUserID.Text = ""
        Me.txtFullName.Text = ""
        Me.txtLogin.Text = ""
        Me.cboGender.Text = ""
        Me.txtPassword.Text = ""
        Me.cboGroup.Text = ""
        Me.PicUsers.Image = Nothing
        Me.PicUsers.BackgroundImage = Nothing
    End Sub
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        SearchDataFromAccess(Me.txtFind.Text)
    End Sub

    Private Sub LVUsers_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LVUsers.MouseDoubleClick
        Dim i As Integer
        For i = 0 To Me.LVUsers.Items.Count - 1
            If LVUsers.Items(i).Selected = True Then
                Me.txtUserID.Text = LVUsers.Items(i).Text
                Me.txtFullName.Text = LVUsers.Items(i).SubItems(1).Text
                Me.txtLogin.Text = LVUsers.Items(i).SubItems(2).Text
                Me.cboGender.Text = LVUsers.Items(i).SubItems(3).Text
                Me.cboGroup.Text = LVUsers.Items(i).SubItems(4).Text
                PicUsers.BackgroundImage = Image.FromFile(Application.StartupPath & "\UserImages\" & LVUsers.Items(i).SubItems(5).Text)
                PicUsers.BackgroundImageLayout = ImageLayout.Stretch
            End If
        Next
        btnDelete.Enabled = True
        BtnEdit.Enabled = True
        BtnSave.Enabled = False
    End Sub

    Private Sub LVUsers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LVUsers.SelectedIndexChanged

    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        Dim SQL As String = "UPDATE tbl_users SET UserName='" & Me.txtFullName.Text & _
                                              "', UserLogin='" & Me.txtLogin.Text & _
                                              "',[Password]='" & Me.txtPassword.Text & _
                                              "',UpdateDate='" & FormatDateTime(Now, DateFormat.ShortDate) & _
                                              "',[Group]='" & cboGroup.Text & "' WHERE UserID=" & Me.txtUserID.Text
        SaveDataToAccess(SQL)
        Clear()
        LoadDataToLVUsers("SELECT * FROM tbl_users WHERE Deleted <>1 AND Status <>0", LVUsers)

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim SQL As String = "UPDATE tbl_users SET [Status]=0,Deleted=1  WHERE UserID=" & Me.txtUserID.Text
        SaveDataToAccess(SQL)
        Clear()
        LoadDataToLVUsers("SELECT * FROM tbl_users WHERE Deleted <>1 AND Status <> 0", LVUsers)
    End Sub
End Class